#include "EdgeDetection.h"

namespace blobDetection
{


unsigned char EdgeDetection::C_R=0x01;
unsigned char EdgeDetection::C_G=0x02;
unsigned char EdgeDetection::C_B=0x04;
//float EdgeDetection::m_coeff = 3.0f*255.0f;
	
//--------------------------------------------
// Constructor
//--------------------------------------------
EdgeDetection::EdgeDetection(int imgWidth, int imgHeight, int bytesPerPixel)
{
	this->imgWidth 	= imgWidth;
	this->imgHeight	= imgHeight;
	init(imgWidth, imgHeight);
	this->bytesPerPixel = bytesPerPixel;

	//colorFlag=C_ALL;
	m_coeff = this->bytesPerPixel * 255.0f;
	posDiscrimination = false;
} 

//--------------------------------------------
// setPosDiscrimination()
//--------------------------------------------
void EdgeDetection::setPosDiscrimination(bool is)
{
	posDiscrimination = is;
}

//--------------------------------------------
// setThreshold()
//--------------------------------------------
void EdgeDetection::setThreshold(float value)
{
	if (value<0.0f) value=0.0f;
	if (value>1.0f) value=1.0f;
	setIsovalue(value*m_coeff);
}

//--------------------------------------------
// setComponent()
//--------------------------------------------
/*
public void setComponent(byte flag)
{
	if (flag==0) flag = C_ALL;
	colorFlag = flag;
}
*/

//--------------------------------------------
// setImage()
//--------------------------------------------
void EdgeDetection::setImage(int* pixels)
{
	this->pixels = pixels;
}

//--------------------------------------------
// setBytesPerPixel()
//--------------------------------------------
void EdgeDetection::setBytesPerPixel(int bpp)
{
	this->bytesPerPixel = bpp;
	m_coeff = this->bytesPerPixel * 255.0f;
}

//--------------------------------------------
// computeEdges()
//--------------------------------------------
void EdgeDetection::computeEdges(int* pixels)
{
	setImage(pixels);
	computeMesh();	
}

//--------------------------------------------
// computeIsovalue()
//--------------------------------------------
void EdgeDetection::computeIsovalue()
{
	 int 	pixel,r,g,b;
	 int 	x,y;
	 int 	offset;
	 
	 r=0;g=0;b=0;
	 for (y=0 ; y<imgHeight;y++)
	   for (x=0 ; x<imgWidth;x++)
	   {
	   	offset = x+imgWidth*y;
	   	
	   	// Add R,G,B
	   	pixel = pixels[offset];
		if( bytesPerPixel == 3)
		{
			r = (pixel & 0x00FF0000)>>16;
			g = (pixel & 0x0000FF00)>>8;
	   	}
		b = (pixel & 0x000000FF);
	   	
	   	gridValue[offset] =  (float) (r+g+b);// /m_coeff   
	   }
}

//--------------------------------------------
// getSquareIndex()
//--------------------------------------------
int EdgeDetection::getSquareIndex(int x, int y)
{
	 int squareIndex = 0;
     int offy  = resx*y;
     int offy1 = resx*(y+1);
     
     if (posDiscrimination == false)
     {
        if (gridValue[x+offy]		< isovalue) squareIndex |= 1;
        if (gridValue[x+1+offy]		< isovalue) squareIndex |= 2;
        if (gridValue[x+1+offy1]	< isovalue) squareIndex |= 4;
        if (gridValue[x+offy1]		< isovalue) squareIndex |= 8;
     }
     else
     {
        if (gridValue[x+offy]		> isovalue) squareIndex |= 1;
        if (gridValue[x+1+offy]		> isovalue) squareIndex |= 2;
        if (gridValue[x+1+offy1]	> isovalue) squareIndex |= 4;
        if (gridValue[x+offy1]		> isovalue) squareIndex |= 8;
     }	
     return squareIndex;
}


//--------------------------------------------
// getEdgeVertex()
//--------------------------------------------
EdgeVertex EdgeDetection::getEdgeVertex(int index)
{
		return edgeVrt[index];
}

};
